package com.yaowen.xmlreader;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class MainActivity extends AppCompatActivity {
    Button SAX = null;
    Button Dom = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SAX = (Button) findViewById(R.id.button1);
        SAX.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                try {
                    SAXParser saxParser = saxParserFactory.newSAXParser();
                    XMLReader xmlReader = saxParser.getXMLReader();
                    SAXHandler saxHandler = new SAXHandler();
                    xmlReader.setContentHandler(saxHandler);
                    try {
                        xmlReader.parse(new InputSource(getAssets().open("F.xml")));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                }
            }
        });
        Dom = (Button) findViewById(R.id.button2);
        Dom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    DomHandler.GetStringFromDom(new InputSource(getAssets().open("F.xml")));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
